

//
//  NDataChangeViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/14.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NDataChangeViewController.h"

@interface NDataChangeViewController ()

@property (weak, nonatomic) IBOutlet UITextField *name;

@property (weak, nonatomic) IBOutlet UITextField *tel;

@property (weak, nonatomic) IBOutlet UIButton *btn;

@property (weak, nonatomic) IBOutlet UIView *bgView;

@end

@implementation NDataChangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"修改联系人信息" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    self.btn.layer.cornerRadius = 5.f;
    self.btn.layer.masksToBounds = YES;
    self.btn.backgroundColor = ThemeBlue;
    
    self.bgView.layer.borderWidth = 0.2f;
    self.bgView.layer.masksToBounds = YES;
    
    self.name.delegate = self;
    self.tel.delegate = self;
}

///修改联系人信息
- (IBAction)btnAction:(id)sender {
    
    if (self.name.text.length==0||self.tel.text.length==0) {
        [AFToast showText:@"请完善信息"];
        return;
    }
    [SVProgressHUD show];
    WeakStament(ws);
    NSDictionary *dic = @{@"identUserId":[NUserModel shareInstance].userInfoId,@"identContact":self.name.text,@"identTelephone":self.tel.text,};
    [[NNRCNetRequest manager]postWithUrl:kChangeContactInfoUrl params:dic success:^(id result) {
        
        YYLog(@"%@",result);
        if ([result[@"code"] integerValue]==0) {
            [SVProgressHUD dismiss];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [AFToast showText:@"修改成功"];
                [ws.navigationController popViewControllerAnimated:YES];
            });
        }
        
    } failure:^(id failure) {
        YYLog(@"%@",failure);
    }];
}


- (void)changeAction:(UIButton *)btn
{
    
   
}


#pragma mark - 字数限制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.name) {
        
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.name.text.length >= 10) {
            self.name.text = [textField.text substringToIndex:10];
            return NO;
        }
    }
    
    return YES;
    
}

#pragma mark - 手机号校验
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField==self.tel) {
       BOOL isTel =  [ToolKit validateMobile:self.tel.text];
        if (!isTel) {
            [AFToast showText:@"请输入正确的手机号"];
            return;
        }
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
